package zhoug.common.imageloader.wrap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import zhoug.common.imageloader.CircleImageTarget;
import zhoug.common.imageloader.RoundedCornerImageTarget;
import zhoug.common.imageloader.RoundedCorners;
import zhoug.common.imageloader.WatermarkTransformation;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.DrawableRes;
import jp.wasabeef.glide.transformations.GrayscaleTransformation;

/**
 * 图片加载工具:最新
 *
 * @Author: zg
 * @Date: 2021/9/7
 * @Description: 图片加载工具
 */
public class GlideHelper {
    private static final String TAG = ">>>GlideHelper";
    private Context mContext;
    private Object url;//图片地址
    private boolean isCircle;//是否是圆形
    private boolean isGrayscale;//是否灰度
    private int corners;//圆角
    private @DrawableRes
    int placeholder;//占位图
    private @DrawableRes
    int error;//错误图
    private @DrawableRes
    int fallback;//图片地址为空时的图
    private boolean isCenterCrop;//ImageView是否设置了CenterCrop,加载圆角图片时需要设置
    private int width;//图片的宽度
    private int height;//图片的高度
    private String watermark;//水印
    /**
     * @param context Context
     * @return this
     */
    public static GlideHelper with(Context context) {
        return new GlideHelper(context);
    }

    /**
     * @param context Context
     */
    private GlideHelper(Context context) {
        this.mContext = context;
    }

    /**
     * 设置图片地址
     *
     * @param url 图片地址
     * @return this
     */
    public GlideHelper load(Object url) {
        this.url = url;
        return this;
    }

    /**
     * 图片转换成圆形
     *
     * @param isCircle 是否是圆形,默认false
     * @return this
     */
    public GlideHelper transformCircle(boolean isCircle) {
        this.isCircle = isCircle;
        return this;
    }

    /**
     * 图片转换成灰色
     *
     * @param isGrayscale 是否是灰色,默认false
     * @return this
     */
    public GlideHelper transformGrayscale(boolean isGrayscale) {
        this.isGrayscale = isGrayscale;
        return this;
    }

    /**
     * 设置图片圆角
     *
     * @param corners      圆角大小 单位dp
     * @param isCenterCrop imageView 是否是CenterCrop
     * @return this
     */
    public GlideHelper setRoundedCorners(int corners, boolean isCenterCrop) {
        this.corners = corners;
        this.isCenterCrop = isCenterCrop;
        return this;
    }

    /**
     * 设置图片圆角,默认设置{@link #isCenterCrop} true
     *
     * @param corners 圆角大小 单位dp
     * @return this
     */
    public GlideHelper setRoundedCorners(int corners) {
        this.corners = corners;
        isCenterCrop = true;
        return this;
    }

    /**
     * 设置占位图,成功加载图片之前显示
     *
     * @param placeholder 占位图资源地址id
     * @return this
     */
    public GlideHelper placeholder(@DrawableRes int placeholder) {
        this.placeholder = placeholder;
        return this;
    }

    /**
     * 设置错误图,图片加载失败后显示
     *
     * @param error 错误图资源id
     * @return this
     */
    public GlideHelper error(@DrawableRes int error) {
        this.error = error;
        return this;
    }

    /**
     * 设置图片地址为空时显示的图片
     *
     * @param fallback 图片地址为空时显示的图片资源id
     * @return this
     */
    public GlideHelper fallback(@DrawableRes int fallback) {
        this.fallback = fallback;
        return this;
    }

    /**
     * 设置ImageView是否是CenterCrop,加载圆角图片时需要设置
     *
     * @param isCenterCrop ImageView是否是CenterCrop
     * @return this
     */
    public GlideHelper setCenterCrop(boolean isCenterCrop) {
        this.isCenterCrop = isCenterCrop;
        return this;
    }

    /**
     * 当网络图片本身像素小于设置的宽高时会显示网络图片的宽高，而当网络图片大于设置的宽高时Glide会进行比例压缩
     *
     * @param width  需要加载的图片的宽度
     * @param height 需要加载的图片的高度
     * @return this
     */
    public GlideHelper override(int width, int height) {
        this.width = width;
        this.height = height;
        return this;
    }

    public GlideHelper addWatermark(String watermark){
        this.watermark=watermark;
        return this;
    }



    /**
     * 开始加载图片
     *
     * @param imageView {@link ImageView} 显示图片的组件
     */
    @SuppressWarnings("unchecked")
    public void into(ImageView imageView) {
        if (url==null || TextUtils.isEmpty(url.toString())) {
            //图片为null
            loadEmpty(imageView);
        } else {
            RequestBuilder<Drawable> requestBuilder = Glide.with(mContext).load(url);
            RequestOptions requestOptions = new RequestOptions();
            if (placeholder != 0) {
                requestOptions = requestOptions.placeholder(placeholder);
            }
            if (error != 0) {
                requestOptions = requestOptions.error(error);
            }
            if (fallback != 0) {
                requestOptions = requestOptions.fallback(fallback);
            }
            //添加,圆形,圆角,灰度转换
            requestOptions = addTransformations(requestOptions);
            if(watermark!=null){
                requestOptions = requestOptions.transform(new WatermarkTransformation(watermark));
            }

            requestBuilder = requestBuilder.apply(requestOptions);
            if (!isCircle && corners > 0) {
                //圆角图片是,占位图和错误图片不是圆角的问题解决
                if (placeholder != 0) {
                    //用缩率图显示占位图
                    requestBuilder = requestBuilder.thumbnail(loadTransform(placeholder));
                }
                if (error != 0) {
                    //加载失败后重新发出请求加载错误图
                    RequestBuilder<Drawable> errorRequestBuilder = Glide.with(mContext)
                            .load(error)
                            .apply(addTransformations(new RequestOptions()));
                    requestBuilder.error(errorRequestBuilder);
                }
            }
            _into(requestBuilder, imageView);
        }
    }


    /**
     * 图片为null时加载提示图
     *
     * @param imageView 显示图片的组件
     */
    private void loadEmpty(ImageView imageView) {
        //url地址为空,显示空图,或者错误图
        int nullImage = 0;
        if (fallback != 0) {
            nullImage = fallback;
        } else if (error != 0) {
            nullImage = error;
        }
        if (nullImage != 0) {
            RequestBuilder<Drawable> requestBuilder = Glide.with(mContext)
                    .load(nullImage)
                    .apply(addTransformations(new RequestOptions()));  //添加,圆形,圆角,灰度转换
            _into(requestBuilder, imageView);
        }
    }


    private void _into(RequestBuilder<Drawable> requestBuilder, ImageView imageView) {
        if (width > 0 || height > 0) {
            requestBuilder = requestBuilder.override(width, height);
        }
        if (isCircle) {
            requestBuilder.into(new CircleImageTarget<>(imageView));
        } else if (corners > 0) {
            requestBuilder.into(new RoundedCornerImageTarget<>(imageView, corners));
        } else {
            requestBuilder.into(imageView);
        }
    }

    /**
     * 添加转换器
     *
     * @param requestOptions {@link RequestOptions}
     * @return
     */
    @SuppressWarnings("all")
    private RequestOptions addTransformations(RequestOptions requestOptions) {
        List<Transformation<Bitmap>> transformations = new ArrayList<>();
        if (isCircle) {
            //圆形
            transformations.add(new CircleCrop());
        } else if (corners > 0) {
            //圆角
            transformations.add(new RoundedCorners(corners, isCenterCrop));
        }
        //灰度
        if (isGrayscale) {
            transformations.add(new GrayscaleTransformation());
        }
        if (transformations.size() > 0) {
            Transformation<Bitmap>[] trans = new Transformation[transformations.size()];
            for (int i = 0; i < transformations.size(); i++) {
                trans[i] = transformations.get(i);
            }
            transformations.clear();
            requestOptions = requestOptions.transform(trans);
        }
        return requestOptions;
    }

    /**
     * 通过thumbnail方法对占位图再做圆角处理
     *
     * @param resId 图片资源id
     * @return this
     */
    private RequestBuilder<Drawable> loadTransform(@DrawableRes int resId) {
        RequestOptions options = RequestOptions.bitmapTransform(new RoundedCorners(corners, isCenterCrop));
        return Glide.with(mContext)
                .load(resId)
                .apply(options);
    }


}
